﻿using AutoMapper;
using Core.SimpleTemp.Application.RoleApp;
using Core.SimpleTemp.Application.SysApp.UserApp.Dto;
using Core.SimpleTemp.Application.UserApp;
using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Mvc.Mobile.Sys
{
    /// <summary>
    /// 用户信息
    /// </summary>
    [ApiExplorerSettings(GroupName = "Mobile")]
    [Route("api/Mobile/[controller]")]
    [EnableCors("any")]
    [ApiController]
    [Authorize]
    public class UserController : CoreApiController
    {
        private readonly SysUserAppService _service;

        private readonly ILogger<UserController> _logger;
        WebAppOptions _webAppOptions;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        /// <param name="sysRoleAppService"></param>
        /// <param name="logger"></param>
        /// <param name="webAppOptions"></param>
        public UserController(SysUserAppService service, SysRoleAppService sysRoleAppService, ILogger<UserController> logger, IOptionsMonitor<WebAppOptions> webAppOptions)
        {
            _service = service;
            _logger = logger;
            _webAppOptions = webAppOptions.CurrentValue;
        }

        /// <summary>
        /// 查看当前用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetCurrUserInfo")]
        public async Task<IActionResult> GetCurrUserInfo()
        {
            var model = await _service.GetAsync(UserIdentity.Id);
            var userDuty = await _service.GetUserDutyAsync(UserIdentity.Id);
            var sysDepartmentName = userDuty.Select(a => a.SysDepartmentDuty.SysDepartment.Name);
            var dto = Mapper.Map<SysUserDto>(model);
            return this.JsonSuccess(new
            {
                dto.Id,
                dto.SysUserInfor.Name,
                PhotoID = dto.SysUserInfor.PhoneID,
                dto.SysUserInfor.WorkCompanyText,
                sysDepartmentName
            });
        }

    }
}